package com.hnd.springframerwork.beans.factory.support;

import com.hnd.springframerwork.beans.BeansException;
import com.hnd.springframerwork.beans.factory.ConfigurableListableBeanFactory;
import com.hnd.springframerwork.beans.factory.config.BeanDefinition;
import com.hnd.springframerwork.beans.factory.registry.BeanDefinitionRegistry;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * 负责管理beanDefinition
 */
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry , ConfigurableListableBeanFactory {
    Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();


    @Override
    public BeanDefinition getBeanDefinition(String beanName) {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) {
            throw new BeansException("no bean define");
        }
        return beanDefinition;
    }

    @Override
    public boolean containsBeanDefinition(String beanName) {
        return beanDefinitionMap.containsKey(beanName);
    }

    @Override
    public BeanDefinition[] getBeanDefinitions() {
        return beanDefinitionMap.values().stream().collect(Collectors.toList()).toArray(new BeanDefinition[0]);
    }

    @Override
    public void registryBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName, beanDefinition);
    }


}
